
(function () {
    const echart = echarts.init(document.getElementById('resourceUseStatusMap'))
    let flag = false
    function first() {
        var o = $.Deferred();
        $.ajax({
            url: info.base_url + '/ccp/test/dynamicSelect',
            type: 'post',
            dataType: 'json',
            //会议室
            data: {
                token: info.token,
                result: 'count(1) used_room,(select count(1)  from t_meeting_room where place_id  =\'9\' and active_status=\'0\' and del_flag =\'0\')  all_room',
                tableName: 't_meeting_room_apply_request',
                condition: '1=1  and  (now() between start_date and end_date ) and pass_flag =\'0\'  and place_id =\'9\' and del_flag=\'1\''
            },
            success: function (res) {
                console.log(res)
                o.resolve(res[0])
            }
        })
        return o.promise()
    }
    function second(greeting) {
        var o = $.Deferred();
        $.ajax({
            url: info.base_url + '/ccp/test/dynamicSelect',
            type: 'post',
            dataType: 'json',
            //商住房
            data: {
                token: info.token,
                result: 'count( distinct room_id ) ,count(distinct room_id)/57',
                tableName: 't_business_room_apply_request',
                condition: "1=1  and  (now() between start_date and end_date ) and pass_flag =\'0\' and identity_card is not null"
            }
        }).done(function (res) {
            o.resolve({ first: greeting, second: res[0] })
        })
        return o.promise()
    }

    function third(greeting) {
        var o = $.Deferred();
        $.ajax({
            url: info.base_url + '/ccp/front/sh/bedapplyAnalysis!execute',
            type: 'post',
            dataType: 'json',
            data: {
                placeId: 9,
                uid: 'bedapplyAnalysis010',
                applyTime: datas.getQueryTime(),
                token: info.token
            },
            success: function (res) {
                datas.bedapplyAnalysis().then(function (gre) {
                    greeting.third = res.beans[0] || {
                        allGraNum: 0,
                        allNum:gre.beans[0].allNum
                    }
                    o.resolve(greeting)
                })

                
            }
        })
        return o.promise()
    }

    function bar() {
        if (flag) {
            return
        }
        flag = true
        var xAxisData = ['会议室', '商住房', '换班宿舍', '充电桩', '坐席'];
        var itemStyle = {
            normal: {
            },
            emphasis: {
                barBorderWidth: 1,
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowOffsetY: 0,
                shadowColor: 'rgba(0,0,0,0.5)'
            }
        };
        var option = {
            legend: {
                data: [
                    '剩余量', '使用量'
                ],
                right: '1%',
                top: '3%',
                textStyle: {
                    color: 'rgba(255,255,255,0.8)'
                }
            },
            tooltip:{
                trigger:'item'
            },
            color: ['#23F9EC', '#225566'],

            splitLine: {
                show: true,
                lineStyle: {
                    color: ['#585858']
                }
            },
            grid: {
                left: '3%',
                right: '3%',
                bottom: '0%',
                top: '20%',
                containLabel: true
            },

            xAxis: {
                data: xAxisData,
                silent: false,
                axisLine: { onZero: true },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#585858'
                    }
                },
                splitArea: { show: false },
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                }
            },
            yAxis: {
                inverse: false,
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#585858'
                    }
                },
            },
            series: [
                {
                    name: '剩余量',
                    type: 'bar',
                    stack: 'one',
                    itemStyle: itemStyle,
                    data: []
                },
                {
                    name: '使用量',
                    type: 'bar',
                    stack: 'one',
                    itemStyle: itemStyle,
                    data: []
                },

            ]
        };

        first().then(second).then(third).then(datas.getCountData).then(function (greeting) {
            option.series[0] = {
                type: 'bar',
                stack: 'one',
                data: [
                    greeting.first.used_room,
                    greeting.second['count( distinct room_id )'],
                    greeting.third.allGraNum,
                    41,
                    {value:(greeting.count.data.seatCount * 74 / 10000).toFixed(2), tooltip:{
                        formatter: "{c}(百)"
                    }}
                ]
            }
            option.series[1] = {
                type: 'bar',
                stack: 'one',
                data: [
                    greeting.first.all_room - greeting.first.used_room,
                    57 - greeting.second['count( distinct room_id )'],
                    greeting.third.allNum,
                    11,
                    {value:(greeting.count.data.seatCount * 26 / 10000).toFixed(2), tooltip:{
                        formatter: "{c}(百)"
                    }}
                ]

            }
            echart.setOption(option)
            flag = false
        })
    }

    function line() {
        if (flag) {
            return
        }
        flag = true
        first().then(second).then(third).then(datas.getCountData).then(function (greeting) {

            const names = ['会议室', '商住房', '换班宿舍', '充电桩', '坐席']
            const use = [
                parseFloat(greeting.first.used_room) / parseFloat(greeting.first.all_room),
                parseFloat(greeting.second['count( distinct room_id )']) / parseFloat(57),
                parseFloat(greeting.third.allGraNum) / parseFloat(greeting.third.allNum),
                (41 / 52).toFixed(2),
                .74
            ]
            const precents = use.map(function (e) {
                return String(e * 100).split('.')[0] + '%'
            })
            for (let i = 0; i < names.length; i++) {
                var node = $('#useItemModal .item').clone()
                node.find('.name').text(names[i])
                node.find('.line1').css({
                    width: precents[i]
                })
                node.find('.precent').text(precents[i])
                node.appendTo($('.useItem'))
            }
            flag = false
        })
    }

    $('.resourceUseStatus button').on('click', function () {
        if ($(this).hasClass('focus')) {
            return
        }
        $(this).removeClass('nofocus').addClass('focus')
        $(this).siblings().removeClass('focus').addClass('nofocus')

        echart.clear()
        if ($(this).attr('id') == '1') {
            line()
            $('.useItem').show()
        } else {
            $('.useItem').empty()
            $('.useItem').hide()
            bar()
        }
    })
    bar()
})()


